<?php
/**
 * 测试设备注册和登录功能
 */

require_once __DIR__ . '/../../vendor/autoload.php';

function logWithTime($msg) {
    echo '[' . date('Y-m-d H:i:s') . '] ' . $msg . "\n";
}

function sendDeviceMessage($host, $port, $cmd, $data) {
    $socket = @stream_socket_client("tcp://{$host}:{$port}", $errno, $errstr, 5);
    if (!$socket) {
        logWithTime("Failed to connect: {$errno} {$errstr}");
        return false;
    }
    
    $marker = "OFZL";
    $version = 0x02;
    $dtype = 0x01;
    
    $jsonData = json_encode($data, JSON_UNESCAPED_UNICODE);
    $header = $marker . chr($version) . chr($dtype) . chr($cmd) . pack('N', strlen($jsonData)); // 使用大端序
    $packet = $header . $jsonData;
    
    fwrite($socket, $packet);
    
    $response = fread($socket, 1024);
    fclose($socket);
    
    if ($response && strlen($response) >= 11) {
        $respLen = unpack('N', substr($response, 7, 4))['1'];
        $respBody = substr($response, 11, $respLen);
        return json_decode($respBody, true);
    }
    
    return false;
}

$host = '127.0.0.1';
$port = 8778;
$testSn = 'TEST_DEVICE_001';

logWithTime("=== 测试设备注册和登录功能 ===");

// 1. 测试设备注册
logWithTime("1. 测试设备注册...");
$registerData = [
    'sn' => $testSn,
    'message' => 'registerReq',
    'messageId' => '1',
    'data' => [
        'dversion' => '4.0.2.001',
        'protocolVersion' => '2'
    ]
];

$registerResult = sendDeviceMessage($host, $port, 0x05, $registerData);
if ($registerResult) {
    logWithTime("注册响应: " . json_encode($registerResult, JSON_UNESCAPED_UNICODE));
    
    if ($registerResult['code'] === 200) {
        $passwd = $registerResult['data']['passwd'];
        logWithTime("设备注册成功，获得密码: {$passwd}");
        
        // 2. 测试设备登录
        logWithTime("2. 测试设备登录...");
        $timestamp = time();
        $sign = md5($testSn . $passwd . $timestamp);
        
        $loginData = [
            'sn' => $testSn,
            'message' => 'loginReq',
            'messageId' => '1',
            'data' => [
                'dversion' => '4.0.2.001',
                'protocolVersion' => '2',
                'sign' => $sign,
                'timestamp' => $timestamp
            ]
        ];
        
        $loginResult = sendDeviceMessage($host, $port, 0x01, $loginData);
        if ($loginResult) {
            logWithTime("登录响应: " . json_encode($loginResult, JSON_UNESCAPED_UNICODE));
            
            if ($loginResult['code'] === 200) {
                logWithTime("设备登录成功！");
                
                // 3. 测试心跳
                logWithTime("3. 测试心跳...");
                $heartbeatData = [
                    'sn' => $testSn,
                    'message' => 'heartbeatReq',
                    'messageId' => '1',
                    'data' => [
                        'status' => 'online',
                        'userCount' => 0
                    ]
                ];
                
                $heartbeatResult = sendDeviceMessage($host, $port, 0x07, $heartbeatData);
                if ($heartbeatResult) {
                    logWithTime("心跳响应: " . json_encode($heartbeatResult, JSON_UNESCAPED_UNICODE));
                }
            } else {
                logWithTime("设备登录失败: " . $loginResult['msg']);
            }
        } else {
            logWithTime("登录请求失败");
        }
    } else {
        logWithTime("设备注册失败: " . $registerResult['msg']);
    }
} else {
    logWithTime("注册请求失败");
}

logWithTime("=== 测试完成 ===");
